﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace CountLines
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private float c_lines = 0;
        private int c_files = 0;
        private int c_dir = 1;

        /// <summary>
        /// 选择文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDirSelect_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (fbd.SelectedPath != "")
                {
                    this.txtDirPath.Text = fbd.SelectedPath;
                }
            }
            
        }

        /// <summary>
        /// 统计代码行数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCountLines_Click(object sender, RoutedEventArgs e)
        {
            string dirPath = this.txtDirPath.Text;
            if (string.IsNullOrEmpty(dirPath))
            {
                MessageBox.Show("请选择需要统计的代码目录！");
                return;
            }
            string strExtensionList = this.txtExtension.Text;
            if (!string.IsNullOrEmpty(strExtensionList))
            {
                strExtensionList = strExtensionList.Replace("，", ",");
            }
            string[] extension = strExtensionList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

            //重置变量
            c_lines = 0;
            c_files = 0;
            c_dir = 1;
            this.lbShow.Items.Clear();
            this.btnDirSelect.IsEnabled = false;
            this.btnCountLines.IsEnabled = false;

            //SumAllFile(dirPath);
            //string result = string.Format("文件夹：{0}，文件：{1}，代码行数：{2}", c_dir, c_files, c_lines);
            //this.lbShow.Items.Add(result);
            Task.Run(() =>
            {
                SumAllFile(dirPath, extension);
                string result = string.Format("文件夹：{0}，文件：{1}，代码行数：{2}", c_dir, c_files, c_lines);
                lbShow.Dispatcher.Invoke(() =>
                {
                    lbShow.Items.Add(result);
                    btnDirSelect.IsEnabled = true;
                    btnCountLines.IsEnabled = true;
                });

            });


        }

        /// <summary>
        /// 统计代码行数
        /// </summary>
        /// <param name="dirPath">需要统计的目录</param>
        private void SumAllFile(string dirPath, string[] extension)
        {
            string[] files = { };
            if (extension.Length == 0)
            {
                files = Directory.GetFiles(@dirPath, "*.*", SearchOption.AllDirectories);
            }
            else
            {
                files = Directory.GetFiles(@dirPath, "*.*", SearchOption.AllDirectories).Where(x => extension.Contains(System.IO.Path.GetExtension(x))).ToArray();
            }
            string[] dirs = Directory.GetDirectories(@dirPath, "*.*", SearchOption.AllDirectories);

            if (files.Length > 0)
            {
                c_dir = dirs.Length;
                c_files = files.Length;

                foreach (var file in files)
                {
                    float lines = 0;
                    using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        using (StreamReader sr = new StreamReader(fs))
                        {
                            string str = sr.ReadLine();
                            while (str != null)
                            {
                                if (!string.IsNullOrEmpty(str))
                                {
                                    lines++;
                                }
                                str = sr.ReadLine();
                            }
                        }
                    }
                    //this.lbShow.Items.Add(file + "|" + lines);
                    lbShow.Dispatcher.Invoke(() =>
                    {
                        lbShow.Items.Add(file + "|" + lines);
                    });

                    c_lines = c_lines + lines;
                }
            }

            //if (dirs.Length > 0)
            //{
            //    c_dir = c_dir + dirs.Length;
            //    foreach (var dir in dirs)
            //    {
            //        SumAllFile(dir);
            //    }
            //}


        }


    }
}
